package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 * @Name: 曾令凯
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;
    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        try {

            //定义一个集合
            List<Setmeal> setmealList = null;

            //redis 查询
            String getMealList = jedisPool.getResource().get("setmealList");
            //判断是否为空
            if (getMealList==null || getMealList.equals("")){
                //空进入 执行查询方法mysql数据库
                System.out.println("数据库查询");
                setmealList = setmealService.findAll();
                //把setmealList变成JSon对象
                Object setmealListObj = JSONObject.toJSON(setmealList);
                //把JSon对象转化为String 在存到redis数据库
                jedisPool.getResource().set("setmealList", String.valueOf(setmealListObj));
            }else{
                //如果有 进行反序列化 放到集合
                setmealList=JSONArray.parseArray(getMealList,Setmeal.class); }

            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    /**
     * 根据id查询套餐详情
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {

            //定义一个空对象
            Setmeal setmeal = null;
            //redis 查询
            String getSetmeal = String.valueOf(jedisPool.getResource().hget("setmeal",String.valueOf(id)));
            //判断是否为空
            if (getSetmeal==null || getSetmeal.equals("[null]") || getSetmeal.equals("") || getSetmeal.equals("null")){
                //空进入 执行查询方法mysql 数据库 在存到redis数据库
                setmeal = setmealService.findById(id);
                Object setmealStr = JSONObject.toJSON(setmeal);//将java对象转换为json对象
                //创建map集合用于存储
                Map<String, String> hash = new HashMap<String, String>();
                //存储到redis数据库hash列表
                hash.put(String.valueOf(id), String.valueOf(setmealStr));
                jedisPool.getResource().hmset("setmeal", hash);
            }else{
                // 先将json格式字符串转换为json对象，再将json对象转换为java对象。
                setmeal = JSON.parseObject(getSetmeal, Setmeal.class);//将json字符串转换为json对象
            }
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
